package com.boge.modules.music.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.boge.common.utils.PageUtils;
import com.boge.common.utils.R;
import com.boge.modules.music.entity.MusicEntity;
import com.boge.modules.music.entity.MusictypeEntity;
import com.boge.modules.music.service.MusicService;
import com.boge.modules.music.service.MusictypeService;
import com.boge.modules.sys.controller.WebController;
import com.boge.modules.sys.entity.SysRoleEntity;
import com.boge.modules.sys.entity.SysUserEntity;
import com.boge.modules.sys.entity.SysUserRoleEntity;
import com.boge.modules.sys.service.SysRoleService;
import com.boge.modules.sys.service.SysUserRoleService;
import com.boge.modules.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class AppController {

    @Autowired
    private SysUserService userService;

    @Autowired
    private SysRoleService roleService;

    @Autowired
    private SysUserRoleService userRoleService;
    @Autowired
    private MusictypeService musictypeService;

    @Autowired
    private MusicService musicService;


    /**
     * 查询所有类别信息
     * @param params
     * @return
     */
    @RequestMapping("/auth/home/info")
    //@RequiresPermissions("house:tarea:list")
    public R list(@RequestParam Map<String, Object> params){
        // 查询所有的类别信息
        List<MusictypeEntity> types = musictypeService.list();
        for (MusictypeEntity type : types) {
            type.setImg(WebController.BASE_URL+type.getImg());
        }
        return R.ok().put("code",200).put("message","success")
                .put("data",types);
    }

    /**
     * 根据类别id查询 音乐信息
     * @param params
     * @return
     */
    @RequestMapping("/auth/home/musicByTypeId")
    //@RequiresPermissions("house:tarea:list")
    public R musicByTypeId(@RequestParam Map<String, Object> params){
        // 查询所有的类别信息
        String typeId = (String)params.get("typeId");
        if(typeId != null){
            List<MusicEntity> list = musicService.list(new LambdaQueryWrapper<MusicEntity>()
                    .eq(MusicEntity::getTypeId, Integer.parseInt(typeId)));
            return R.ok().put("code",200).put("message","success")
                    .put("data",list);
        }
        return R.ok().put("code",200).put("message","success")
                .put("data",null);
    }

    /**
     * 查询所有的音乐信息
     * @param params
     * @return
     */
    @RequestMapping("/auth/home/musicList")
    //@RequiresPermissions("house:tarea:list")
    public R musicList(@RequestParam Map<String, Object> params){
        List<MusicEntity> list = musicService.list();
        return R.ok().put("code",200).put("message","success")
                .put("data",list);

    }

    /**
     * 注册用户
     * @param user
     * @return
     */
    @PostMapping("/auth/registerApp")
    @ResponseBody
    public R registerApp(@RequestBody SysUserEntity user){
        user.setStatus(1);
        //user.setPassword("123456");
        if(StringUtils.isBlank(user.getNickname())){
            user.setNickname(user.getUsername());
        }

        // 绑定角色
        userService.saveUser(user);
        QueryWrapper<SysRoleEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("role_name","用户");
        List<SysRoleEntity> list = roleService.list(wrapper);
        if(list != null && list.size() > 0){
            SysRoleEntity sysRoleEntity = list.get(0);
            SysUserRoleEntity entity = new SysUserRoleEntity();
            entity.setUserId(user.getUserId());
            entity.setRoleId(sysRoleEntity.getRoleId());
            userRoleService.save(entity);
        }
        return R.ok();
    }
}
